using System;
using System.Collections;
using System.Drawing;

namespace PocketPC_AdventureGame
{
	/// <summary>
	/// Summary description for Inventory.
	/// </summary>
	public class Inventory
	{
		public const int ICON_SIZE = 20;

		private ArrayList contents = new ArrayList(10);
		private Pen backgoundbrush = new Pen(Color.Black);
		public Rectangle position;


		public Inventory(Rectangle position)
		{
			this.position = position;
		}

		public void Add( GameObject obj ) 
		{
			if( !contents.Contains(obj) ) 
			{
				contents.Add(obj);
			}
			LayoutObjects();
		}

		public void Remove( GameObject obj ) 
		{
			contents.Remove(obj);
			LayoutObjects();
		}
		/// <summary>
		/// Returns the GameObject at the given position in the inventory, ot null if no object at position.
		/// </summary>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <returns></returns>
		public GameObject HitTest( int x, int y ) 
		{
			foreach( GameObject obj in contents ) 
			{
				if( obj.HitTest(x,y) ) 
				{
					return obj;
				}
			}
			return null;
		}

		private void LayoutObjects() 
		{
			Rectangle goalPos = new Rectangle( position.X+5 , position.Bottom-ICON_SIZE-10, ICON_SIZE, ICON_SIZE );
			foreach( GameObject obj in contents ) 
			{
				obj.MoveTo(goalPos);
				goalPos.Y -= (ICON_SIZE+10);
			}

		}

		public void Update(double timeSinceLastUpdate) 
		{
            foreach (GameObject obj in contents) obj.Update(timeSinceLastUpdate);
		}

		public void Draw(Graphics g) 
		{
			g.DrawRectangle(backgoundbrush, position);
			foreach( GameObject obj in contents ) obj.Draw(g);
		}

	}
}
